package cn.wolfcode.luowowo.ticket.service.impl;

import cn.wolfcode.luowowo.cache.service.IVerifyCodeCacheService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.common.util.Assert;
import cn.wolfcode.luowowo.ticket.domain.Dest;
import cn.wolfcode.luowowo.ticket.domain.Subject;
import cn.wolfcode.luowowo.ticket.domain.Ticket;
import cn.wolfcode.luowowo.ticket.mapper.DestMapper;
import cn.wolfcode.luowowo.ticket.mapper.SubjectMapper;
import cn.wolfcode.luowowo.ticket.mapper.TicketMapper;
import cn.wolfcode.luowowo.ticket.service.ITicketService;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class TicketServiceImpl implements ITicketService {
    @Autowired
    private TicketMapper ticketMapper;
    @Autowired
    private SubjectMapper subjectMapper;
    @Autowired
    private DestMapper destMapper;
    @Reference
    private IVerifyCodeCacheService verifyCodeCacheService;

    @Override
    public List<Ticket> listTicketTop4() {
        return ticketMapper.selectTop4();
    }

    @Override
    public List<Ticket> selectTicksByDestIdAndSubjectId(Long ajaxDestId, Long ajaxSubjectId) {
        return ticketMapper.selectTicksByDestIdAndSubjectId(ajaxDestId, ajaxSubjectId);
    }

    @Override
    public List<Subject> selectSubjectsByDestId(Long ajaxDestId) {
        return ticketMapper.selectSubjectsByDestId(ajaxDestId);
    }


    @Override
    public AjaxResult checkPhoneAndCode(String phone, String verifyCode) {
        AjaxResult result = new AjaxResult();
        try {
            //1.非空检测
            Assert.hasLength(verifyCode, "验证码不能为空");
            Assert.hasLength(phone, "手机号不能为空");

            //2.验证码判断
            String verifyCodeTicket4Redis = verifyCodeCacheService.getVerifyCode4Ticket(phone);
            Assert.equalsIgnoreCase(verifyCode, verifyCodeTicket4Redis, "验证码有误");

        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    @Override
    public Ticket selectById(Long tid) {
        return ticketMapper.selectByPrimaryKey(tid);
    }

    @Override
    public List<Ticket> selectTicketTop5ByDestId(Long tid) {
        return ticketMapper.selectTop5ByDestId(tid);
    }

    @Override
    public List<Ticket> selectTicketTop5BySubjectId(Long ajaxSubjectId) {
        return ticketMapper.selectTop5BySubjectId(ajaxSubjectId);
    }

    @Override
    public Subject selectSubjectByTicketId(Long tid) {
        return subjectMapper.selectByTicketId(tid);
    }

    @Override
    public Dest selectDestByTicketId(Long tid) {
        return destMapper.selectByTicketId(tid);
    }

    @Override
    public Ticket selectCoordinateById(Long id) {
        return ticketMapper.selectCoordinateById(id);
    }
}
